﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataLogicLayer
/// </summary>
public class DataLogicLayer
{
	public DataLogicLayer()
	{
		//
		// TODO: Add constructor logic here
		//
	}


    /// <summary>
    ///
    /// </summary>
    /// <param name="SingleTag"></param>
    /// <param name="TagCount"></param>
    /// <returns></returns>
    public bool SetTagCount(string SingleTag, int TagCount)
    {
        return ExecuteNonQueryCommand(DataLogicSQL.SetTagCountSQL(SingleTag, TagCount));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="SingleTag"></param>
    /// <returns></returns>
    public bool UpdateTagCount(string SingleTag)
    {
        int x = GetTagCount(SingleTag);
        if (SetTagCount(SingleTag, x + 1))
        {
            return true;
        }
        else
        {
           return false;
        }
        
    }

    public bool InsertANewTag(string SingleTag)
    {
        return ExecuteNonQueryCommand(DataLogicSQL.InsertNewTagSQL(SingleTag));
    }
    public DataTable getBlogDataTable()
    {
        DataTable dt = RunQuery(DataLogicSQL.GetBlogReadSQL()).Tables[0];
        return dt;   
    }
    public int GetTagCount(string SingleTag)
    {
        DataSet ds = RunQuery(DataLogicSQL.GetTagCountSQL(SingleTag));
        int PrevCount = Int32.Parse(ds.Tables[0].Rows[0]["TagCount"].ToString());
        return PrevCount;
    }
    public DataTable GetAllTagInfo()
    {
        DataSet ds = RunQuery(DataLogicSQL.GetAllTagsSQL());
        return ds.Tables[0];
    }
    public int[] GetMaxMinTagCount()
    {
        int []MaxMin= new int[2];
        DataSet ds = RunQuery(DataLogicSQL.GetMaxMinSQL());
        MaxMin[0]=(int)ds.Tables[0].Rows[0]["MaxTagCount"];
        MaxMin[1]=(int) ds.Tables[0].Rows[0]["MinTagCount"];
        return MaxMin;
    }
    public DataTable GetNewsTable(bool IsPublished)
    {
        DataSet ds = RunQuery(DataLogicSQL.GetNewsSQL(IsPublished));
        return ds.Tables[0];
    }
    
    public DataTable GetContentTableByPageAndItem(string page, string item)
    {
        DataSet ds = RunQuery(DataLogicSQL.GetConectByPageAndItemSQL(page,item));
        return ds.Tables[0];
    }

    public DataSet RunQuery(SqlCommand sqlQuery)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["Personal"].ConnectionString.ToString();
        SqlConnection DBConnection = new SqlConnection(connectionString);
        SqlDataAdapter dbAdapter = new SqlDataAdapter();
        dbAdapter.SelectCommand = sqlQuery;
        sqlQuery.Connection = DBConnection;
        DataSet resultsDataSet = new DataSet();
        try
        {
            dbAdapter.Fill(resultsDataSet);
        }
        catch (Exception ex)
        {
            //Label1.Text = ex.Message.ToString();
        }
        finally
        {
            DBConnection.Close();
        }
        return resultsDataSet;
    }

    public bool ExecuteNonQueryCommand(SqlCommand command)
    {
        SqlConnection DBConnection = null;
        try
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Personal"].ConnectionString.ToString();
            DBConnection = new SqlConnection(connectionString);
            DBConnection.Open();
            command.Connection = DBConnection;
            if (command.ExecuteNonQuery() > 0)
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            DBConnection.Close();
        }
    }
}
